home *** CD-ROM | disk | FTP | other *** search
- unit FileSysThread;
-
- interface
-
- uses
- Windows, SysUtils, Classes, comctrls;
-
- type
- TFileSysNotifyThread = class(TThread)
- private
- ErrCode: Integer;
- KillAddress: PInteger;
- NotifyHandle: THandle;
- WatchPath: String;
- WatchMask: Integer;
- procedure SignalFileNotification;
- protected
- procedure Execute; override;
- public
- constructor Create (const AWatchPath: String; AWatchMask: Integer; var Myself: TFileSysNotifyThread);
- destructor Destroy; override;
- end;
-
- implementation
-
- uses Dialogs;
-
- constructor TFileSysNotifyThread.Create (const AWatchPath: String; AWatchMask: Integer; var Myself: TFileSysNotifyThread);
- begin
- // Thread is created in suspended state
- Inherited Create (True);
- // Save various stuff for later
- WatchPath := AWatchPath;
- WatchMask := AWatchMask;
- KillAddress := Addr (Myself);
- // Priority is lower than normal
- Priority := tpLower;
- // Set thread to free itself when terminated
- FreeOnTerminate := True;
- // Let slip the dogs of war...
- Suspended := False;
- end;
-
- destructor TFileSysNotifyThread.Destroy;
- begin
- if NotifyHandle <> THandle (-1) then FindCloseChangeNotification (NotifyHandle);
- Inherited Destroy;
- KillAddress^ := 0; // Very last thing we do....
- end;
-
- procedure TFileSysNotifyThread.Execute;
- begin
- // Create the synchronisation object
- NotifyHandle := FindFirstChangeNotification (PChar (WatchPath), False, WatchMask);
- // No synchronisation object, no comment...
- if NotifyHandle <> THandle (-1) then while not Terminated do begin
- ErrCode := WaitForSingleObject (NotifyHandle, 250);
- // Was it a timeout, or something more interesting ?
- case ErrCode of
- Wait_Timeout: // Just a timeout -- ignore it....
- ;
- Wait_Object_0: // We've got a valid change notification
- begin
- Synchronize (SignalFileNotification);
- FindNextChangeNotification (NotifyHandle);
- end;
-
- else ; // Something deeply bad has happened....
- end;
- end;
- end;
-
- procedure TFileSysNotifyThread.SignalFileNotification;
- begin
- ShowMessage ('Something''s changed!');
- end;
-
- end.
-